using System;
using System.Globalization;
using System.Threading;

namespace InfoJetSoft.Service.Util
{
	
	public class NumberFormat
	{
		public static readonly string Number = "number";
		public static readonly string Double = "double";
		public static readonly string Percentage = "percentage";
		public static readonly string Currency = "currency";
		
		public string CurrencySymbol {
			get {
				return this.currencySymbol;
			}
			
			set {
				this.currencySymbol = value;
			}
			
		}

		public string CurrencyLocale
		{
			get 
			{
				return this.currencyLocale;
			}
			
			set 
			{
				this.currencyLocale = value;
			}
			
		}

		
		public CultureInfo Culture
		{
			get 
			{
				return this.culture;
			}
			
			set 
			{
				this.culture = value;
			}
			
		}

		public string Type {
			get {
				return this.type;
			}
			
			set {
				this.type = value;
			}
			
		}

		public bool GroupingUsed {
			get {
				return this.groupingUsed;
			}
			
			set {
				this.groupingUsed = value;
			}
			
		}

		public int MinFractionDigits {
			get {
				return this.minFractionDigits;
			}
			
			set {
				this.minFractionDigits = value;
			}
		}

		public int NegativePattern {
			get {
				return this.negativePattern;
			}
			
			set {
				this.negativePattern = value;
			}
		}

		public int PositivePattern {
			get {
				return this.positivePattern;
			}
			
			set {
				this.positivePattern = value;
			}
		}

		public NumberFormat()
		{
			this.minFractionDigits = -1;
		}

		public string Format(double value_Renamed){
			NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
			string format = "";
			if( this.minFractionDigits == -1 )
			{
				string decimalPoint = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
				string strvalue = value_Renamed + "";
				while( true )
				{
					if( strvalue.EndsWith( "0" ) )
					{
						strvalue = strvalue.Substring( 0, strvalue.Length - 1 );
					}
					else
					{
						break;
					}
				}
				if( strvalue.EndsWith( decimalPoint ) )
				{
					strvalue = strvalue.Substring( 0, strvalue.Length - 1 );
					this.minFractionDigits = 0;
				}
				else
				{
					if( strvalue.LastIndexOf( decimalPoint ) >= 0 )
					{
						this.minFractionDigits = strvalue.Length - strvalue.LastIndexOf( decimalPoint ) - 1;
					}
					else
					{
						this.minFractionDigits = 0;
					}
				}
			}
            if (!groupingUsed)
            {
                numberFormatInfo.NumberGroupSizes = new int[] { 0 };
            }
            else
            {
                numberFormatInfo.NumberGroupSizes = new int[] { 3 };
            }
			if( this.type.Equals( NumberFormat.Number ) ||
				this.type.Equals( NumberFormat.Double ) || 
				this.type.Equals( NumberFormat.Percentage ) ){
				format = "N";
				numberFormatInfo.NumberDecimalDigits = this.minFractionDigits;
				numberFormatInfo.NumberNegativePattern = this.NegativePattern;
				//CID 0471 //2009.11.30 //Begin
				numberFormatInfo.NumberDecimalSeparator = this.culture.NumberFormat.NumberDecimalSeparator;
				numberFormatInfo.NumberGroupSeparator = this.culture.NumberFormat.NumberGroupSeparator;
				//CID 0471 //2009.11.30 //End
			}else if( this.type == NumberFormat.Currency ){
				format = "C";
				numberFormatInfo.CurrencyDecimalDigits = this.minFractionDigits;
				numberFormatInfo.CurrencyNegativePattern = this.NegativePattern;
				numberFormatInfo.CurrencyPositivePattern = this.PositivePattern;
				numberFormatInfo.CurrencySymbol = this.currencySymbol;
				//CID 0471 //2009.12.06 //Begin
				numberFormatInfo.CurrencyDecimalSeparator = this.culture.NumberFormat.CurrencyDecimalSeparator;
				numberFormatInfo.CurrencyGroupSeparator = this.culture.NumberFormat.CurrencyGroupSeparator;
				//CID 0471 //2009.12.06 //End
			}
			string formatedValue = value_Renamed.ToString( format , numberFormatInfo );
			return formatedValue;
		}

		private bool groupingUsed = true;
		private int minFractionDigits = 0;
		private int negativePattern = 0;
		private int positivePattern = 0;
		private string type = null;
		private string currencySymbol = null;
		private string currencyLocale = null;
		private CultureInfo culture = null;
	}
}
